//
//  BHStadiumDetailHeaderView.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/7/1.
//

import UIKit

class BHStadiumDetailHeaderView: UIView {

    @IBOutlet weak var banner_coverView: UIView!
    @IBOutlet weak var content_view: UIView!
    
    @IBOutlet weak var coverImageView: UIImageView!
    @IBOutlet weak var name_label: UILabel!
    
    @IBOutlet weak var area_label: UILabel!
    
    @IBOutlet weak var distance_label: UILabel!
    
    @IBOutlet weak var time_label: UILabel!
    
    @IBOutlet weak var address_label: UILabel!
    
    @IBOutlet weak var phoneBtn: UIButton!
    
    lazy var bannerView: BHCycleView = {
        let view = BHCycleView.init()
        view.backgroundColor = .color_f8f8f8
        view.collectionView.backgroundColor = .color_f8f8f8
        view.scrollDirection = .horizontal
        view.timerValue = 8
        view.leadingAndTralingSpace = 0
        view.lineSpace = 0
        view.dataSource = self
        view.delegate = self
        return view
    }()
    
    static func loadFromXib() -> BHStadiumDetailHeaderView {
        let viewArray = Bundle.main.loadNibNamed("BHStadiumDetailHeaderView", owner: nil, options: nil)
        let view: BHStadiumDetailHeaderView = viewArray![0] as! BHStadiumDetailHeaderView
        return view
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        self.addSubview(self.bannerView)
        self.bannerView.mas_makeConstraints { (make) in
            make?.left.right()?.top()?.bottom()?.equalTo()(self.coverImageView)
        }
        self.coverImageView.addViewAction {[weak self] (_) in
            if self?.model!.imageModelArray.count == 1 { // 有图
                BHMediaTool.showPreview(array: self?.model!.imageModelArray ?? [], current: 0)
            }
        }
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        if self.content_view.haveRaiuds == false  {
            self.content_view.addCorner(conrners: [.topLeft,.topRight], radius: 12)
            self.content_view.haveRaiuds = true
        }
    }
    
    static var headerHeight: CGFloat {
        get {
            let bannerHeight: CGFloat = CGFloat(274).radius
            let contentHeight = CGFloat(186)-12
            return (bannerHeight+contentHeight)
        }
    }
    
    
    var model: BHStadiumDetailModel? {
        didSet {
            if model == nil {
                return
            }
            self.name_label.text = model!.name
            self.area_label.text = model!.cityName+"/"+model!.regionName
            self.time_label.text = model!.startTime+"至"+model!.endTime
            self.address_label.text = model!.address
//            self.distance_label.text = model.
            if model!.imageModelArray.count <= 1 {
                self.bannerView.isHidden = true
                if model!.imageModelArray.count == 0 {
                    self.coverImageView.sd_setImage(with: URL.init(string: model!.cover), placeholderImage: nil, options: .retryFailed, completed: nil)
                }else {
                    let imageModel = model!.imageModelArray.first
                    
                    self.coverImageView.sd_setImage(with: URL.init(string: imageModel?.imageUrl ?? ""), placeholderImage: nil, options: .retryFailed, completed: nil)
                }
            }else {
                self.bannerView.isHidden = false
                self.bannerView.sourceArray = model!.imageModelArray
            }
            
        }
    }
    
}
extension BHStadiumDetailHeaderView: BHCycleViewDataSource,BHCycleViewDelegate {
    func cellForRow(_ indexPath: IndexPath, _ collectionView: UICollectionView,_ cycleView: UIView) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BHCommonBannerImageCell", for: indexPath) as! BHCommonBannerImageCell
        if self.model != nil && self.model?.imageModelArray.count != 0 {
            let imageModel = self.model!.imageModelArray[indexPath.item%self.model!.imageModelArray.count]
            cell.bannerImageUrl = imageModel.imageUrl ?? ""
        }
    
        cell.backgroundColor = .clear
        return cell
    }
    func sizeForItem(_ indexPath: IndexPath,_ cycleView: UIView) -> CGSize {
        return CGSize(width: UIScreen.width, height: CGFloat(274).radius)
    }
    
    func numberOfItem(_ cycleView: UIView) -> Int {
        if self.model == nil {
            return 0
        }
        return self.model!.imageModelArray.count
    }
    func didSelectItem(_ index: Int,_ cycleView: UIView) {
        BHMediaTool.showPreview(array: self.model!.imageModelArray, current: index)
    }
}
